package mof.gov.dmdc.model.applicationmodule.extra;

import java.sql.CallableStatement;

import java.sql.SQLException;
import java.sql.Types;

import mof.gov.dmdc.model.viewobject.extra.tabmisint.TabmisValueSetIntView2Impl;

import oracle.jbo.JboException;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.ViewObjectImpl;

import oracle.jdbc.OracleTypes;

import oracle.sql.BFILE;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Mon Nov 07 14:20:37 ICT 2011
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class COAModuleImpl extends ApplicationModuleImpl {

    private static int BFILE = OracleTypes.BFILE;
    private static int VARCHAR2 = Types.VARCHAR;

    /**
     * This is the default constructor (do not remove).
     */
    public COAModuleImpl() {
    }

    /**
     * Container's getter for TabmisValueSetIntView.
     * @return TabmisValueSetIntView
     */
    public ViewObjectImpl getTabmisValueSetIntView() {
        return (ViewObjectImpl)findViewObject("TabmisValueSetIntView");
    }

    /**
     * Container's getter for COAMoniter.
     * @return COAMoniter
     */
    public ViewObjectImpl getCOAMoniter() {
        return (ViewObjectImpl)findViewObject("COAMoniter");
    }

    public BFILE getBFILELocator(String dir, String fileName) {
        return (BFILE)callStoreProcedure(BFILE, "GET_BFILE_LOCATOR(?, ?)", new Object[] { dir, fileName });
    }

    protected Object callStoreProcedure(int sqlReturnType, String stmt, Object[] bindVars) {
        /**
         * SAMPLE: (String)callStoredFunction(VARCHAR2,"KNS_PKG_WEB_AUTH.checkLogin(?,?)",
         *                                              new Object[]{u,p});
         */
        CallableStatement st = null;
        try {
            // 1. Create a JDBC CallabledStatement
            st = getDBTransaction().createCallableStatement("begin ? := " + stmt + ";end;", 0);
            // 2. Register the first bind variable for the return value
            st.registerOutParameter(1, sqlReturnType);
            if (bindVars != null) {
                // 3. Loop over values for the bind variables passed in, if any
                for (int z = 0; z < bindVars.length; z++) {
                    // 4. Set the value of user-supplied bind vars in the stmt
                    if (bindVars[z] == null) {
                        st.setObject(z + 2, "");
                    } else {
                        st.setObject(z + 2, bindVars[z]);
                    }
                }
            }
            // 5. Set the value of user-supplied bind vars in the stmt
            st.executeUpdate();
            // 6. Return the value of the first bind variable
            return st.getObject(1);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            throw new JboException(e);
        } finally {
            if (st != null) {
                try {
                    // 7. Close the statement
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public String checkStatusCodeByType(String pCode, String pSegmentName, String pSessionId) {
        return (String)callStoreProcedure(VARCHAR2, "DMDC_PCK_COA_ADMIN.checkStatusCodeByType(?, ?, ?)", new String[] { pCode, pSegmentName, pSessionId });
    }

    public String getScalar(String sqlStr) {
        return (String)callStoreProcedure(VARCHAR2, "DMDC_PCK_COA_ADMIN.getScalar(?)", new String[] { sqlStr });
    }

    public String deleteSessionData(String sessId) {
        return (String)callStoreProcedure(VARCHAR2, "DMDC_PCK_COA_ADMIN.deleteSession(?)", new String[] { sessId });
    }

    public String insertCheckData(String lookupCode, String tmpTen, String tmpTuNgay, String tmpDenNgay, String vSTATUS, String sDNS, String sTMP, String sDMDC,
                                  String sDMDCEXT, String sTABMIS, String vERROR_CODE, String vERROR_DESCRIPTION, String ghiChu, String pLoai, String pSessionId) {
        return (String)callStoreProcedure(VARCHAR2, "DMDC_PCK_COA_ADMIN.insertCheckData(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                                          new String[] { lookupCode, tmpTen, tmpTuNgay, tmpDenNgay, vSTATUS, sDNS, sTMP, sDMDC, sDMDCEXT, sTABMIS, vERROR_CODE,
                                                         vERROR_DESCRIPTION, ghiChu, pLoai, pSessionId });
    }


    /**
     * Container's getter for LogTransTextView1.
     * @return LogTransTextView1
     */
    public ViewObjectImpl getLogTransTextView1() {
        return (ViewObjectImpl)findViewObject("LogTransTextView1");
    }
}
